Path: blob/master/src/packages/next/pages/share/public_paths/[...id].tsx
5808 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { join } from "path";6import NextHead from "next/head";78import basePath from "lib/base-path";9import getPublicPathInfo from "lib/share/get-public-path-info";10import shareURL from "lib/share/share-url";11import withCustomize from "lib/with-customize";12import { getPublicPathNames } from "lib/names/public-path";13import PublicPath, { PublicPathProps } from "components/path/path";1415import ogShareLogo from "public/logo/og-share-logo.png";1617export default (props: PublicPathProps) => (18<>19<PublicPath {...props} />20<NextHead>21<meta property="og:type" content="article" />22<meta property="og:title" content={props.path} />2324{props.description && (25<meta property="og:description" content={props.description} />26)}27{props.ogUrl && <meta property="og:url" content={props.ogUrl} />}28{props.ogImage && <meta property="og:image" content={props.ogImage} />}29{props.created && (30<meta property="article:published_time" content={props.created} />31)}32{props.last_edited && (33<meta property="article:modified_time" content={props.last_edited} />34)}3536{/* Prevent search engine indexing of unlisted content or proxied content from external URLs */}37{(props.unlisted ||38(props.url &&39(props.url.startsWith("github/") ||40props.url.startsWith("gist/")))) && (41<>42<meta name="robots" content="noindex, nofollow" />43<meta name="googlebot" content="noindex, nofollow" />44</>45)}46</NextHead>47</>48);4950export async function getServerSideProps(context) {51const id = context.params.id[0];52const relativePath = context.params.id.slice(1).join("/");53try {54const names = await getPublicPathNames(id);55if (names != null) {56// redirect57let location = join(58basePath,59names.owner,60names.project,61names.public_path,62);63if (context.params.id.length > 1) {64location = join(65location,66"files",67context.params.id.slice(1).join("/"),68);69}70return { props: { redirect: location } };71}72const props: PublicPathProps = await getPublicPathInfo({73id,74relativePath,75req: context.req,76});7778const customize = await withCustomize({ context, props });7980if (customize?.props?.customize != null) {81// Add full URL for social media sharing82//83customize.props.ogUrl = `${customize.props.customize.siteURL}${shareURL(84id,85relativePath,86)}`;8788// Add image path for social media sharing89//90customize.props.ogImage =91customize.props.customize.logoSquareURL ||92`${customize.props.customize.siteURL}${ogShareLogo.src}`;93}9495return customize;96} catch (_err) {97console.log(_err);98return { notFound: true };99}100}101102103